// src/stores/carDetailStores.ts
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useCarDetailStore = defineStore('carDetail', {
  state: () => ({
    carDetail: {
      cdid: 0,
      productyear: '',
      level: '',
      seats: 0,
      doors: 0,
      energyid: 0,
      gearid: 0,
      displacement: '',
      drive: 0,
      upwindow: 0,
      radar: 0,
      gps: 0,
      carid: 0,
      lisence: 0,
      fsfile: 0,
      isdeleted: 0,
      isonline: 0,
      userid: 0,
      plate: '',
      photos: []
    },
    energyTypes: [
      { id: 1, name: '汽油' },
      { id: 2, name: '柴油' },
      { id: 3, name: '电动' },
      { id: 4, name: '混合动力' }
    ],
    gearTypes: [
      { id: 1, name: '手动' },
      { id: 2, name: '自动' },
      { id: 3, name: '无级变速' },
      { id: 4, name: '双离合' }
    ],
    driveTypes: [
      { id: 1, name: '前驱' },
      { id: 2, name: '后驱' },
      { id: 3, name: '四驱' }
    ]
  }),
  
  getters: {
    // 获取车辆详情
    getCarDetail: (state) => state.carDetail,
    
    // 获取车辆图片数组
    getPhotos: (state) => state.carDetail.photos.map(photo => photo.cpimg),
    
    // 获取能源类型名称
    getEnergyTypeName: (state) => {
      return (energyId: number) => {
        const energy = state.energyTypes.find(type => type.id === energyId)
        return energy ? energy.name : ''
      }
    },
    
    // 获取变速箱类型名称
    getGearTypeName: (state) => {
      return (gearId: number) => {
        const gear = state.gearTypes.find(type => type.id === gearId)
        return gear ? gear.name : ''
      }
    },
    
    // 获取驱动方式名称
    getDriveTypeName: (state) => {
      return (driveId: number) => {
        const drive = state.driveTypes.find(type => type.id === driveId)
        return drive ? drive.name : ''
      }
    },
    
    // 获取车辆基本信息
    getBasicInfo: (state) => {
      return {
        productYear: state.carDetail.productyear,
        level: state.carDetail.level,
        seats: state.carDetail.seats,
        doors: state.carDetail.doors,
        displacement: state.carDetail.displacement
      }
    },
    
    // 获取车辆配置信息
    getConfiguration: (state) => {
      return {
        upwindow: state.carDetail.upwindow,
        radar: state.carDetail.radar,
        gps: state.carDetail.gps,
        lisence: state.carDetail.lisence
      }
    },
    
    // 获取车辆状态信息
    getStatusInfo: (state) => {
      return {
        isdeleted: state.carDetail.isdeleted,
        isonline: state.carDetail.isonline,
        fsfile: state.carDetail.fsfile
      }
    }
  },
  
  actions: {
    // 设置车辆详情数据
    setCarDetail(detail: any) {
      this.carDetail = {
        cdid: detail.cdid || 0,
        productyear: detail.productyear || '',
        level: detail.level || '',
        seats: detail.seats || 0,
        doors: detail.doors || 0,
        energyid: detail.energyid || 0,
        gearid: detail.gearid || 0,
        displacement: detail.displacement || '',
        drive: detail.drive || 0,
        upwindow: detail.upwindow || 0,
        radar: detail.radar || 0,
        gps: detail.gps || 0,
        carid: detail.carid || 0,
        lisence: detail.lisence || 0,
        fsfile: detail.fsfile || 0,
        isdeleted: detail.isdeleted || 0,
        isonline: detail.isonline || 0,
        userid: detail.userid || 0,
        plate: detail.plate || '',
        photos: detail.photos || []
      }
    },
    
    // 清空车辆详情数据
    clearCarDetail() {
      this.carDetail = {
        cdid: 0,
        productyear: '',
        level: '',
        seats: 0,
        doors: 0,
        energyid: 0,
        gearid: 0,
        displacement: '',
        drive: 0,
        upwindow: 0,
        radar: 0,
        gps: 0,
        carid: 0,
        lisence: 0,
        fsfile: 0,
        isdeleted: 0,
        isonline: 0,
        userid: 0,
        plate: '',
        photos: []
      }
    },
    
    // 更新车辆详情的某个字段
    updateCarDetailField(field: string, value: any) {
      if (field in this.carDetail) {
        (this.carDetail as any)[field] = value
      }
    },
    
    // 添加图片
    addPhoto(photo: { cpid: number; cpimg: string; cdid: number }) {
      this.carDetail.photos.push(photo)
    },
    
    // 删除图片
    removePhoto(cpid: number) {
      this.carDetail.photos = this.carDetail.photos.filter(photo => photo.cpid !== cpid)
    }
  },
  
  persist: {
    key: 'car-detail-store',
    storage: sessionStorage,
    paths: ['carDetail']
  }
})